<?php

	if(!class_exists(class_manage))
		{
			exit('Nhin cai gi');
		}

	/*========================================/*
	*| Class xu ly cua phan shop
	*| Bao gom cac Ham:
	*| =======================================/*
	*| list_san_pham_shop($page, $result, $shop_id, $category='')
	*| sua_san_pham($id, $array)
	*| list_dat_hang($page, $result)
	*| dat_hang($id, $array)
	*| =======================================/*
	*| Table: user_shop =>
	*| Chua thong tin chuyen muc rieng
	/*========================================*/
		
	class class_shop extends class_manage
		{
		
			/*========================================/*
			*| Ham lay thong tin category rieng cua shop
			*| Function: shop_rieng_info()
			/*========================================*/	
			function shop_rieng_info($userid)
				{
					$userid = intval($userid);
					$query = $this->data->query("SELECT id,tieu_de FROM user_shop WHERE user_id='$userid' AND trangthai=1");
					
					// Return variable
					$return = array();
					
					while($list = $query->fetch_assoc())
						{
						array_push($return, $list);
						}
					return $return;
				}
		
			/*========================================/*
			*| Ham lay ra list sam pham cua shop do
			*| Page la trang dang xem, result la kqua/trang
			*| Ham rieng: list_san_pham_shop_rieng
			/*========================================*/			
			function list_san_pham_shop_rieng($page=1, $result=10, $shop_id, $category)
				{
					$page = intval($page);
					$result = intval($result);
					$shop_id = intval($shop_id);
					$category = intval($category);
					
					// Phan trang
					$trang = $page*$result-$result;		
					
					// Return variable
					$return = array();
					
					// Select shop rieng option
					$user_shop = $this->data->queryFirst("SELECT sanpham FROM user_shop WHERE id='$category' AND user_id='$shop_id' AND trangthai=1");
					
					// Select list sp
					$list_san_pham = $this->data->query("SELECT * FROM sanpham WHERE id IN (".$user_shop['sanpham'].") AND user_id='$shop_id' LIMIT $trang,$result");
					
					while($list = @$list_san_pham->fetch_assoc())
						{
							array_push($return, $list);
						}
					return $return;
				}
			
			
			/*========================================/*
			*| Ham lay ra list sam pham cua shop do
			*| Page la trang dang xem, result la kqua/trang
			*| Template name cho phan list nay ten la: list_san_pham_shop
			/*========================================*/			
			function list_san_pham_shop($page, $result, $shop_id, $category='')
				{
						$page = intval($page);
						$result = intval($result);
						$shop_id = intval($shop_id);
						
						// Phan trang
						$trang = $page*$result-$result;
						
						// Build optional category query
						if($category!='' or !empty($category))
							{
								$category = intval($category);
								$category_query = "(main_cat='".$category."' OR sub1='".$category."' OR sub2='".$category."')";
							}
						else
							{
								$category_query = "1=1";
							}
						
						// Truy van san pham tu shop
						$san_pham = $this->data->query("SELECT * FROM sanpham WHERE user_id='$shop_id' AND ".$category_query." ORDER BY datepost DESC LIMIT $trang, $result");
					
					// Return variable
					$return = array();
					
					// Fetch result by array
					while($list_san_pham = $san_pham->fetch_assoc())
					{
						// Tra ve
						array_push($return, $list_san_pham);
					}
					return $return;
				}
				

				
			/*========================================/*
			*| Ham lay ra list don hang tu table: dat_hang
			*| Page la trang dang xem, result la kqua/trang
			*| Template name cho phan list nay ten la: list_dat_hang
			/*========================================*/			
				
				function list_dat_hang($page, $result)
					{
						$page = intval($page);
						$result = intval($result);
						
						// User_id
						$user_info = $this->load('class_member')->user_info('user_id');
						
						// Phan trang
						$trang = $page*$result-$result;
						
						// Truy van san pham tu shop
						$dat_hang = $this->data->query("SELECT * FROM dat_hang WHERE nguoi_ban='$user_info[user_id]' ORDER BY datepost DESC LIMIT $trang, $result");
						
						// Return variable
						$return = array();
						
						while($list_san_pham = mysql_fetch_array($dat_hang))
						{
						// Tra ve cung template
						array_push($return, $list_san_pham);
						}
					return $return;
					}
			/*========================================/*
			*| Ham dat_hang
			*| $id la id san pham
			*| Ham su dung de nguoi dung dat hang
			*| Array nhap vao se tuong ung voi cac key
			*| Se replace cac array tuong ung trong data
			/*========================================*/			
				function dat_hang($id, $array)
					{
						if(is_array($array))
						{
						$id = intval($id);
						// Lay thong tin ng ban
						$nguoi_ban = mysql_fetch_array(mysql_query("SELECT * FROM sanpham WHERE id='$id'"));
							
							foreach($array as $key => $value)
								{
									$column .= $key.',';
									$value = addslashes($value);
									$new_value .= $value.',';
								}
							if($this->load('class_member')->check_login())
							{
							$user_id = $this->load('class_member')->user_info('user_id');
							$this->data->query("INSERT INTO dat_hang (".$column."nguoi_ban,nguoi_mua) VALUES (".$new_value."$nguoi_ban[user_id],$user_id[user_id])");
							}
							else
							{
							$this->data->query("INSERT INTO dat_hang (".$column."nguoi_ban) VALUES (".$new_value."$nguoi_ban[user_id])");
							}
							return $this->language('dat_hang_thanh_cong');
						}
						else
						{
							return $this->language('input_sai');
						}
					}
					
			/*========================================/*
			*| Ham san_pham_shop_rieng()
			*| input: id cua chuyen muc rieng, id san pham, type=1 la them (mac dinh), type = 0 la xoa
			*| Tac dung: Them san pham vao shop rieng
			*| Language key su dung: khong_phai_chuyen_muc_cua_ban, them_san_pham_rieng_thanh_cong
			/*========================================*/	
			function san_pham_shop_rieng($id_chuyenmuc, $id_sanpham, $type=1)
				{
					// input filter
					$id_chuyenmuc = intval($id_chuyenmuc);
					$id_sanpham = intval($id_sanpham);
					
					if($id_chuyenmuc>=1 or $id_sanpham>=1)
					{
						// Query
						$query = $this->data->query("SELECT sanpham FROM user_shop WHERE id='$id_chuyenmuc' AND user_id='".$this->load('class_member')->userid."' LIMIT 0,1");
						$query_sp = $this->data->queryFirst("SELECT user_id FROM sanpham WHERE id='$id_sanpham'");

						// Check chuyen muc ton tai
						if( $query->num_rows < 1 or $query_sp['user_id']!=$this->load('class_member')->userid )
							{
								return $this->language_normal('khong_phai_chuyen_muc_cua_ban');
							}
						else
							{
							$chuyen_muc = $query->fetch_assoc();
							
								if(empty($chuyen_muc['sanpham']) and $type==1)
									{
									$this->data->query("UPDATE user_shop SET sanpham='\'$id_sanpham\'' WHERE id='$id_chuyenmuc'");
									return $this->language_normal('them_san_pham_rieng_thanh_cong');
									}
								elseif(preg_match("/'$id_sanpham'/", $chuyen_muc['sanpham']))
									{
										if($type==0)
											{
											$san_pham = addslashes(str_replace('\''.$id_sanpham.'\'', '', $chuyen_muc['sanpham']));
											$this->data->query("UPDATE user_shop SET sanpham='$san_pham' WHERE id='$id_chuyenmuc'");
											return $this->language_normal('xoa_san_pham_rieng_thanh_cong');
											}
									return $this->language_normal('them_san_pham_rieng_thanh_cong');
									}
								else
									{
									$san_pham = addslashes($chuyen_muc['sanpham']);
									$this->data->query("UPDATE user_shop SET sanpham='$san_pham,\'$id_sanpham\'' WHERE id='$id_chuyenmuc'");								
									//$this->data->query("UPDATE user_shop SET sanpham='".$chuyen_muc['sanpham'].',\''.$id_sanpham.'\''."' WHERE id='$id_chuyenmuc'");
									return $this->language_normal('them_san_pham_rieng_thanh_cong');
									}
							}
					}
					else
					{
						return $this->language_normal('input_sai');
					}	
				}
			
		}

?>